package com.taomeishu.mailcrazy.fetch;

import java.util.regex.Pattern;

import com.taomeishu.mailcrazy.CheckResult;
import com.taomeishu.mailcrazy.MailExistChecker;
import com.taomeishu.mailcrazy.fetch.policy.DelayPolicy;
import com.taomeishu.mailcrazy.fetch.policy.SavePolicy;
import com.taomeishu.mailcrazy.seq.CharseqNumber;

public class FecthWorker {

	private MailExistChecker mailExistChecker;
	
	private DelayPolicy delayPolicy;
	
	/**
	 * 指向下一个需要处理的username
	 */
	private CharseqNumberManger charseqNumberManger;
	
	private SavePolicy savePolicy;
	
	private String domain;
	
	private CharseqNumber charseqNumber;
	
	private String startString = "100";
	
	public FecthWorker(MailExistChecker mailExistChecker, DelayPolicy delayPolicy, SavePolicy savePolicy, String domain, CharseqNumberManger charseqNumberManger) {
		super();
		this.mailExistChecker = mailExistChecker;
		this.delayPolicy = delayPolicy;
		this.savePolicy = savePolicy;
		this.domain = domain;
		this.charseqNumberManger = charseqNumberManger;
	}
	
	private FecthWorker() {
		super();
		this.charseqNumber = new CharseqNumber();
	}
	
	public CheckResult execute() throws Exception {
		CheckResult checkResult = null;
		Exception exception = null;
		try {
			delayPolicy.sleepForDelay();
			checkResult = mailExistChecker.checkMail(charseqNumber.toCharSeqString(), domain);
			turnToNextCharseq();
			return checkResult;
		} catch (Exception e) {
			exception = e;
			throw e;
		} finally {
			if (savePolicy.needSave(checkResult, exception)) {
				save();
			}
		}
	}

	public void turnToNextCharseq() {
		while (true) {
			charseqNumber.incr();
			String charseq = charseqNumber.toCharSeqString();
			if (!ignoreString(charseq)) {
				break;
			}
		}
	}
	
	private static final Pattern last_num_pattern = Pattern.compile("[a-z\\-\\.\\_]*\\d*");
	
	protected boolean ignoreString(String charseq) {
		if (!last_num_pattern.matcher(charseq).matches()) {
			return true;
		}
		if (charseq.contains("-")) {
			return true;
		}
		return false;
	}
	
	public static void main(String[] args) {
		FecthWorker f = new FecthWorker();
		for (int i = 0; i < 10000; ++i) {
			f.turnToNextCharseq();
			String s = f.getCharseqNumber().toCharSeqString();
			System.out.println(s);
		}
	}
	
	public void init() {
		charseqNumber = charseqNumberManger.load(domain);
		if (charseqNumber == null) {
			charseqNumber = new CharseqNumber(startString);
		}
	}
	
	public void save() {
		charseqNumberManger.save(domain, charseqNumber);
	}
	
	public MailExistChecker getMailExistChecker() {
		return mailExistChecker;
	}

	public void setMailExistChecker(MailExistChecker mailExistChecker) {
		this.mailExistChecker = mailExistChecker;
	}

	public DelayPolicy getDelayPolicy() {
		return delayPolicy;
	}

	public void setDelayPolicy(DelayPolicy delayPolicy) {
		this.delayPolicy = delayPolicy;
	}

	public CharseqNumber getCharseqNumber() {
		return charseqNumber;
	}

	public void setCharseqNumber(CharseqNumber charseqNumber) {
		this.charseqNumber = charseqNumber;
	}

	public CharseqNumberManger getCharseqNumberManger() {
		return charseqNumberManger;
	}

	public String getDomain() {
		return domain;
	}
	
}
